package com.shawn.mobliesafe;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.shawn.engine.ContactEngine;
import com.shawn.utils.MyAsycnTaks;

import java.util.List;
import java.util.Map;

public class ContactActivity extends AppCompatActivity {

    @ViewInject(R.id.lv_contact_contacts)
    private ListView lv_contact_contacts;
    @ViewInject(R.id.pb_contact_loading)
    private ProgressBar pb_contact_loading;
    private List<Map<String,String>> list;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_contact);

        //lv_contact_contacts = (ListView)findViewById(R.id.lv_contact_contacts);
        ViewUtils.inject(this);

        new MyAsycnTaks(){
            @Override
            public void preTask() {
                pb_contact_loading.setVisibility(View.VISIBLE);
            }

            @Override
            public void doinBack() {
                list = ContactEngine.getAllContactInfo(getApplicationContext());
            }

            @Override
            public void postTask() {
                lv_contact_contacts.setAdapter(new MyAdapter());
                pb_contact_loading.setVisibility(View.INVISIBLE);
            }
        }.execute();

        lv_contact_contacts.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent();
                intent.putExtra("number",list.get(position).get("phone"));
                setResult(RESULT_OK,intent);
                finish();
            }
        });
    }

    public class MyAdapter extends BaseAdapter{
        @Override
        public int getCount() {
            return list.size();
        }

        @Override
        public Object getItem(int position) {
            return list.get(position);
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            if(convertView == null) {
                convertView = View.inflate(ContactActivity.this,R.layout.item_contact,null);
            }

            TextView tv_itemcontact_name = (TextView) convertView.findViewById(R.id.tv_itemcontact_name);
            TextView tv_itemcontact_phone = (TextView) convertView.findViewById(R.id.tv_itemcontact_phone);
            tv_itemcontact_name.setText(list.get(position).get("name"));
            tv_itemcontact_phone.setText(list.get(position).get("phone"));

            return convertView;
        }
    }
}
